#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

class Solution{

public:
    vector<int> arraysIntersection(vector<int> &arr1, vector<int> &arr2, vector<int> &arr3){
        // 结果数组
        vector<int> ans;
        vector<int> x(3, 0);
        x[0] = arr1[0];
        x[1] = arr2[0];
        x[2] = arr3[0];
        // 三个指针遍历三个数组
        int i = 0, j = 0, k = 0;
        while (i < arr1.size() && j < arr2.size() && k < arr3.size())
        {
            if (x[0] == x[1] && x[1] == x[2]) // 三个元素都相等
            {
                if (ans.empty() || x[0] != ans.back())
                {
                    ans.push_back(x[0]);
                    i++, j++, k++;
                    // 如果没有遍历到边界的话，就把元素往后移动
                    if (i < arr1.size())
                    {
                        x[0] = arr1[i];
                    }
                    if (j < arr2.size())
                    {
                        x[1] = arr2[j];
                    }
                    if (k < arr3.size())
                    {
                        x[2] = arr3[k];
                    }
                    
                    
                    
                }
                }else{
                    // 不是三个元素都相等
                    int min = minx(x);
                    switch (min)
                    {
                    case 0:
                        i++;
                        if (i < arr1.size())
                        {
                            x[0] = arr1[i];
                        }
                        break;
                    case 1:
                    j ++;
                    if (j < arr2.size())
                    {
                        x[1] = arr2[j];
                    }
                    break;
                    case 2:
                    k++;
                    if (k < arr3.size())
                    {
                        x[2] = arr3[k];
                    }
                    
                    
                    
                    default:
                        break;
                    }
                
            }
            
        }
        return ans;
        
    }
    int minx(vector<int> &x){
        // 取出x中最小的段号
        int mini = 0;
        for (int i = 1; i < 3; i++)
        {
            if (x[i] < x[mini])
            {
                mini = i;
            }
            
        }
        return mini;
        
    }

};

int main(){

    Solution sol;
    vector<int> arr1 = {1, 2, 3, 4, 5};
    vector<int> arr2 = {2, 3, 5, 6};
    vector<int> arr3 = {1, 2, 5, 7};

    vector<int> result = sol.arraysIntersection(arr1, arr2, arr3);

    for (int num : result) {
        cout << num << " ";
    }

    return 0;
}